package com.imooc.concurrency.example.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class CountDownLatchTest1 {

    static int threadCount = 200;

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();

        CountDownLatch countDownLatch = new CountDownLatch(threadCount);
        for(int i = 0;i < threadCount;i ++) {
            final int num = i;
            executorService.execute(() -> {
                try {
                    test(num);
                } catch (InterruptedException e) {
                    log.info("exception {}", e);
                }finally {
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await();
        log.info("Done");
        executorService.shutdown();
    }

    public static void test(int num) throws InterruptedException {
        log.info("num: {}", num);
        Thread.sleep(100);
    }

}
